#pragma once


#include<vector>
#include<string>
#include<iostream>

using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class T>
struct RBTreeNode
{
	T _data;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;

	RBTreeNode(const T& data)
		:_data(data)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{ }
};

template<class T, class Ref, class Ptr>
struct __TreeItreator
{
	typedef RBTreeNode<T> Node;
	typedef __TreeItreator<T, Ref, Ptr> Self;

	Node* _node;
	Node* _root;

	__TreeItreator(Node* node, Node* root)
		:_node(node)
		,_root(root)
	{ }

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	Self& operator++()
	{
		if (_node->_right)
		{
			_node = _node->_right;
			while (_node->_left)
			{
				_node = _node->_left;
			}
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	Self& operator--()
	{
		if (_node == nullptr)
		{
			Node* cur = _root;
			while (cur && cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		else if (_node->_left)
		{
			_node = _node->_left;
			while (_node->_right)
			{
				_node = _node->_right;
			}
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	bool operator!=(const Self& s)
	{
		return s._node != _node;
	}

	bool operator==(const Self& s)
	{
		return s._node == _node;
	}
};

template<class K, class T, class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef __TreeItreator<T, T&, T*> Iterator;
	typedef __TreeItreator<T, const T&, const T*> ConstIterator;

	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur, _root);
	}

	Iterator End()
	{
		return Iterator(nullptr, _root);
	}

	ConstIterator Begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return ConstIterator(cur, _root);
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr, _root);
	}

	void RotateR(Node* parent)
	{
		Node* sub_left = parent->_left;
		Node* sub_left_right = sub_left->_right;
		Node* parent_parent = parent->_parent;
		parent->_left = sub_left_right;
		if (sub_left_right)
		{
			sub_left_right->_parent = parent;
		}
		sub_left->_right = parent;
		parent->_parent = sub_left;
		if (parent_parent == nullptr)
		{
			_root = sub_left;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_left;
			}
			else
			{
				parent_parent->_right = sub_left;
			}
		}
		sub_left->_parent = parent_parent;
	}

	void RotateL(Node* parent)
	{
		Node* sub_right = parent->_right;
		Node* sub_right_left = sub_right->_left;
		Node* parent_parent = parent->_parent;
		parent->_right = sub_right_left;
		if (sub_right_left)
		{
			sub_right_left->_parent = parent->_right;
		}
		sub_right->_left = parent;
		parent->_parent = sub_right;
		if (parent_parent == nullptr)
		{
			_root = sub_right;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_right;
			}
			else
			{
				parent_parent->_right = sub_right;
			}
		}
		sub_right->_parent = parent_parent;
	}

	pair<Iterator, bool> Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return { Iterator(_root, _root),true };
		}
		Node* cur = _root;
		Node* parent = nullptr;
		KeyOfT kot;
		while (cur)
		{
			if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return { Iterator(cur, _root), false };
			}
		}
		cur = new Node(data);
		Node* new_node = cur;
		cur->_col = RED;
		if (kot(data) > kot(parent->_data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;
		while (parent && parent->_col == RED)
		{
			Node* parent_parent = parent->_parent;
			if (parent == parent_parent->_left)
			{
				Node* uncle = parent_parent->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					parent_parent->_col = RED;
					cur = parent_parent;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(parent_parent);
						parent->_col = BLACK;
						parent_parent->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(parent_parent);
						cur->_col = BLACK;
						parent_parent->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = parent_parent->_left;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					parent_parent->_col = RED;
					cur = parent_parent;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(parent);
						RotateL(parent_parent);
						cur->_col = BLACK;
						parent_parent->_col = RED;

					}
					else
					{
						RotateL(parent_parent);
						parent->_col = BLACK;
						parent_parent->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return { Iterator(new_node, _root),true };
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	Iterator Find(const K& key)
	{
		Node* cur = _root;
		KeyOfT kot;
		while (cur)
		{
			if (key > kot(cur->_data))
			{
				cur = cur->_right;
			}
			else if (key < kot(cur->_data))
			{
				cur = cur->_left;
			}
			else
			{
				return Iterator(cur, _root);
			}
		}
		return End();
	}

	bool Check(Node* root, int black_sum, int reference_val)
	{
		if (root == nullptr)
		{
			if (black_sum != reference_val)
			{
				return false;
			}
			return true;
		}
		if (root->_col == RED && root->_parent && root->_parent->_col == RED)
		{
			return false;
		}
		if (root->_col == BLACK)
		{
			black_sum++;
		}
		return Check(root->_left, black_sum, reference_val)
			&& Check(root->_right, black_sum, reference_val);
	}

	bool IsBalanceTree()
	{
		if (_root == nullptr)
		{
			return true;
		}
		if (_root->_col == RED)
		{
			return false;
		}
		int reference_val = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				reference_val++;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, reference_val);
	}
private:
	/*void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << ' ';
		_InOrder(root->_right);
	}*/

	Node* _root = nullptr;
};